package com.crp.unitalinforbaseplat.app.sys.service.impl;

import com.anhuishangjue.praduationppmsystem.Utils.EmptyUtil;
import com.crp.unitalinforbaseplat.app.core.Message;
import com.crp.unitalinforbaseplat.app.core.MessageUtil;
import com.crp.unitalinforbaseplat.app.sys.dao.PermissionMapper;
import com.crp.unitalinforbaseplat.app.sys.entity.Permission;
import com.crp.unitalinforbaseplat.app.sys.entity.Role;
import com.crp.unitalinforbaseplat.app.sys.service.PermissionService;
import com.crp.unitalinforbaseplat.app.unitalinforbaseplat.entity.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionMapper permissionMapper;


    @Override
    public PageBean<Permission> list(PageBean<Permission> pageBean) {
        pageBean.setData(permissionMapper.list(pageBean));
        return pageBean;
    }

    @Override
    public Message saveOrUpdate(Permission permission) {
        if(EmptyUtil.Companion.isNullOrEmpty(permission.getId())){
            Permission check = permissionMapper.selectByName(permission.getName());
            if(check!= null){
                return MessageUtil.createMessage(false,"角色已经存在");
            }else{
                permissionMapper.insertSelective(permission);
                return MessageUtil.createMessage(true,"新增成功");
            }
        }else{
            Permission check = permissionMapper.selectByPrimaryKey(permission.getId());
            if(check == null){
                return MessageUtil.createMessage(false,"角色不存在");
            }else{
                permissionMapper.updateByPrimaryKeySelective(permission);
                return MessageUtil.createMessage(true,"更新成功");
            }
        }
    }

    @Override
    public Message delete(Long[] id) {
        for (Long tmp:id
             ) {
            permissionMapper.deleteByPrimaryKey(tmp.intValue());
        }
        return MessageUtil.createMessage(true,"删除成功");
    }

    @Override
    public Permission updateInit(Long id) {
        return permissionMapper.selectByPrimaryKey(id.intValue());
    }
}
